package cn.kgc.vue.service.impl;

import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.common.ResponseStatus;
import cn.kgc.vue.dto.RolePermissionInfor;
import cn.kgc.vue.entity.Permission;
import cn.kgc.vue.entity.RolePers;
import cn.kgc.vue.entity.UserRole;
import cn.kgc.vue.mapper.PermissionMapper;
import cn.kgc.vue.mapper.RolePersMapper;
import cn.kgc.vue.mapper.UserRoleMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.vue.entity.Role;
import cn.kgc.vue.service.RoleService;
import cn.kgc.vue.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author
 * @description 针对表【role】的数据库操作Service实现
 * @createDate 2023-08-12 14:10:04
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
        implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RolePersMapper rolePersMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public ResponseResult delete(String id) {
        ResponseResult responseResult = new ResponseResult();
        LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRole::getRoleId, id);
        List<UserRole> list = userRoleMapper.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            responseResult.fail(ResponseStatus.ROLE_USER);
            return responseResult;
        }
        roleMapper.deleteById(id);
        //删除角色和权限表
        LambdaQueryWrapper<RolePers> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(RolePers::getRoleId, id);
        rolePersMapper.delete(wrapper1);
        responseResult.success(ResponseStatus.DELETE_SUCCESS);
        return responseResult;
    }

    @Override
    public ResponseResult addPermission(RolePermissionInfor rolePermissionInfor) {
        //增加权限
        ResponseResult responseResult = new ResponseResult();
        //先删除
        LambdaQueryWrapper<RolePers> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RolePers::getRoleId, rolePermissionInfor.getRoleId());
        rolePersMapper.delete(wrapper);
        Integer roleId = rolePermissionInfor.getRoleId();
        rolePermissionInfor.getPerId().forEach(s -> {
            RolePers rolePers = new RolePers();
            rolePers.setRoleId(roleId);
            rolePers.setPerId(s);
            rolePersMapper.insert(rolePers);
        });
        responseResult.success(ResponseStatus.ALLOCATION_SUCCESS);
        return responseResult;
    }

    @Override
    public ResponseResult showper(Integer roleId) {
        ResponseResult responseResult = new ResponseResult();
        LambdaQueryWrapper<RolePers> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RolePers::getRoleId, roleId);
        List<RolePers> rolePers = rolePersMapper.selectList(wrapper);
        if (!ObjectUtils.isEmpty(rolePers)) {
            List<Integer> collect = rolePers.stream().map(s -> s.getPerId()).collect(Collectors.toList());
            LambdaQueryWrapper<Permission> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.in(Permission::getId, collect).eq(Permission::getIsMenu, 2);
            List<Permission> permissions = permissionMapper.selectList(wrapper1);
            List<Integer> collect1 = permissions.stream().map(s -> s.getId()).collect(Collectors.toList());
            responseResult.success(ResponseStatus.SUCCESS, collect1);
            return responseResult;
        }
        responseResult.success(ResponseStatus.SUCCESS,new ArrayList<>());
        return responseResult;
    }
}




